﻿using System;
using System.Text;
using System.Data.SqlClient;
using System.Collections.Generic;
using System.Data;
using DingTalkAgent.DBUtility;

namespace DingTalkAgent.SqlData
{
    public partial class SysNav
    {
        #region	 T_SysNav

        ///<summary>
        /// Exist
        ///</summary>
        public bool Exists(int Nav_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select count(1) from T_SysNav");
            strSql.Append(" where ");
            strSql.Append(" Nav_ID = @Nav_ID  ");
            SqlParameter[] parameters = {
                    new SqlParameter("@Nav_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Nav_ID;

            return DbHelperSQL.Exists(strSql.ToString(), parameters);
        }



        /// <summary>
        /// 增加一条T_SysNav数据
        /// </summary>
        public int Add(DingTalkAgent.Model.SysNav model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("insert into T_SysNav(");
            strSql.Append("active,Site_ID,Page_ID,Code,Parent_ID,Name,Ioc,Alt,OrderID");
            strSql.Append(") values (");
            strSql.Append("@active,@Site_ID,@Page_ID,@Code,@Parent_ID,@Name,@Ioc,@Alt,@OrderID");
            strSql.Append(") ");
            strSql.Append(";select @@IDENTITY");
            SqlParameter[] parameters = {
                        new SqlParameter("@active", SqlDbType.Bit,1) ,
                        new SqlParameter("@Site_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Page_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Code", SqlDbType.VarChar,12) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Ioc", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Alt", SqlDbType.VarChar,128) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4)

            };

            parameters[0].Value = model.active;
            parameters[1].Value = model.Site_ID;
            parameters[2].Value = model.Page_ID;
            parameters[3].Value = model.Code;
            parameters[4].Value = model.Parent_ID;
            parameters[5].Value = model.Name;
            parameters[6].Value = model.Ioc;
            parameters[7].Value = model.Alt;
            parameters[8].Value = model.OrderID;

            object obj = DbHelperSQL.GetSingle(strSql.ToString(), parameters);
            if (obj == null)
            {
                return 0;
            }
            else
            {

                return Convert.ToInt32(obj);

            }

        }


        /// <summary>
        /// 更新一条T_SysNav数据
        /// </summary>
        public bool Update(DingTalkAgent.Model.SysNav model)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("update T_SysNav set ");

            strSql.Append(" active = @active , ");
            strSql.Append(" Site_ID = @Site_ID , ");
            strSql.Append(" Page_ID = @Page_ID , ");
            strSql.Append(" Code = @Code , ");
            strSql.Append(" Parent_ID = @Parent_ID , ");
            strSql.Append(" Name = @Name , ");
            strSql.Append(" Ioc = @Ioc , ");
            strSql.Append(" Alt = @Alt , ");
            strSql.Append(" OrderID = @OrderID  ");
            strSql.Append(" where Nav_ID=@Nav_ID ");

            SqlParameter[] parameters = {
                        new SqlParameter("@Nav_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@active", SqlDbType.Bit,1) ,
                        new SqlParameter("@Site_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Page_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Code", SqlDbType.VarChar,12) ,
                        new SqlParameter("@Parent_ID", SqlDbType.Int,4) ,
                        new SqlParameter("@Name", SqlDbType.VarChar,64) ,
                        new SqlParameter("@Ioc", SqlDbType.VarChar,256) ,
                        new SqlParameter("@Alt", SqlDbType.VarChar,128) ,
                        new SqlParameter("@OrderID", SqlDbType.Int,4)

            };

            parameters[0].Value = model.Nav_ID;
            parameters[1].Value = model.active;
            parameters[2].Value = model.Site_ID;
            parameters[3].Value = model.Page_ID;
            parameters[4].Value = model.Code;
            parameters[5].Value = model.Parent_ID;
            parameters[6].Value = model.Name;
            parameters[7].Value = model.Ioc;
            parameters[8].Value = model.Alt;
            parameters[9].Value = model.OrderID;
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 删除一条T_SysNav数据
        /// </summary>
        public bool Delete(int Nav_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysNav ");
            strSql.Append(" where Nav_ID=@Nav_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Nav_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Nav_ID;


            int rows = DbHelperSQL.ExecuteSql(strSql.ToString(), parameters);
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 删除一条T_SysNav数据
        /// </summary>
        public bool DeleteList(string Nav_IDlist)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("delete from T_SysNav ");
            strSql.Append(" where ID in (" + Nav_IDlist + ")  ");
            int rows = DbHelperSQL.ExecuteSql(strSql.ToString());
            if (rows > 0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }


        /// <summary>
        /// 得到一个对象实体
        /// </summary>
        public DingTalkAgent.Model.SysNav GetModel(int Nav_ID)
        {

            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Nav_ID, active, Site_ID, Page_ID, Code, Parent_ID, Name, Ioc, Alt, OrderID  ");
            strSql.Append("  from T_SysNav ");
            strSql.Append(" where Nav_ID=@Nav_ID");
            SqlParameter[] parameters = {
                    new SqlParameter("@Nav_ID", SqlDbType.Int,4)
};
            parameters[0].Value = Nav_ID;


            DingTalkAgent.Model.SysNav model = new DingTalkAgent.Model.SysNav();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);

            if (ds.Tables[0].Rows.Count > 0)
            {
                if (ds.Tables[0].Rows[0]["Nav_ID"].ToString() != "")
                {
                    model.Nav_ID = int.Parse(ds.Tables[0].Rows[0]["Nav_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["active"].ToString() != "")
                {
                    if ((ds.Tables[0].Rows[0]["active"].ToString() == "1") || (ds.Tables[0].Rows[0]["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }
                if (ds.Tables[0].Rows[0]["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(ds.Tables[0].Rows[0]["Site_ID"].ToString());
                }
                if (ds.Tables[0].Rows[0]["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(ds.Tables[0].Rows[0]["Page_ID"].ToString());
                }
                model.Code = ds.Tables[0].Rows[0]["Code"].ToString();
                if (ds.Tables[0].Rows[0]["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(ds.Tables[0].Rows[0]["Parent_ID"].ToString());
                }
                model.Name = ds.Tables[0].Rows[0]["Name"].ToString();
                model.Ioc = ds.Tables[0].Rows[0]["Ioc"].ToString();
                model.Alt = ds.Tables[0].Rows[0]["Alt"].ToString();
                if (ds.Tables[0].Rows[0]["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(ds.Tables[0].Rows[0]["OrderID"].ToString());
                }

                return model;
            }
            else
            {
                return null;
            }
        }


        /// <summary>
        /// 获得数据T_SysNav列表
        /// </summary>
        public DataSet GetList(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select * ");
            strSql.Append(" FROM T_SysNav ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(strSql.ToString());
        }

        /// <summary>
        /// 获得数据T_SysNav列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.SysNav> IEnumGetList(string strWhere,int Role_ID)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Nav_ID, active, Site_ID, a.Page_ID, Code, Parent_ID, Name, Ioc, Alt, OrderID ,ISNULL(b.DataPower,0) as DataPower FROM dbo.T_SysNav a left join (select * from dbo.T_Roles_DataPower where Role_ID="+Role_ID+") b on a.Page_ID=b.Page_ID");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by OrderID Desc ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.SysNav model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysNav();
                if (dr["Nav_ID"].ToString() != "")
                {
                    model.Nav_ID = int.Parse(dr["Nav_ID"].ToString());
                }
                if (dr["active"].ToString() != "")
                {
                    if ((dr["active"].ToString() == "1") || (dr["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }
                if (dr["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(dr["Site_ID"].ToString());
                }
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                model.Code = dr["Code"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.Ioc = dr["Ioc"].ToString();
                model.Alt = dr["Alt"].ToString();
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }
                if (dr["DataPower"].ToString() != "")
                {
                    model.DataPower = int.Parse(dr["DataPower"].ToString());
                }
                yield return model;
            }
        }
        /// <summary>
        /// 获得数据T_SysNav列表
        /// </summary>
        public IEnumerable<DingTalkAgent.Model.SysNav> IEnumGetListX(string strWhere)
        {
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Nav_ID, active, Site_ID, a.Page_ID, Code, Parent_ID, Name, Ioc, Alt, OrderID,b.Action,b.Controller ");
            strSql.Append(@" FROM T_SysNav a  
                            Left join (select Page_ID,Controller,Action from T_SysPage) b on a.Page_ID=b.Page_ID 
                            ");
            if (strWhere.Trim() != "")
            {
                strSql.Append(" where " + strWhere);
            }
            strSql.Append(" order by OrderID Desc ");
            DataSet ds = DbHelperSQL.Query(strSql.ToString());
            DingTalkAgent.Model.SysNav model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysNav();
                if (dr["Nav_ID"].ToString() != "")
                {
                    model.Nav_ID = int.Parse(dr["Nav_ID"].ToString());
                }
                if (dr["active"].ToString() != "")
                {
                    if ((dr["active"].ToString() == "1") || (dr["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }
                if (dr["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(dr["Site_ID"].ToString());
                }
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                model.Code = dr["Code"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.Url = "/" + dr["Controller"].ToString() + "/" + dr["Action"].ToString();
                model.Ioc = dr["Ioc"].ToString();
                model.Alt = dr["Alt"].ToString();
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }

                yield return model;
            }
        }

        /// <summary>
        /// 根据Role获得Nav
        /// </summary>
        /// <param name="Role"></param>
        /// <returns></returns>
        public IEnumerable<DingTalkAgent.Model.SysNav> IEnumBuildNav(int Role)
        {
            StringBuilder strSql = new StringBuilder();

            strSql.Append(@" SELECT [Nav_ID]
                                  ,[Parent_ID]
                                  ,[Name]
                                  ,[Alt]
                                  ,Ioc
	                              ,[Controller]
	                              ,[Action]
                              FROM [T_SysNav] Nav
                              left join (
		                            SELECT a.[Page_ID]
		                              ,a.[Name] pageName
		                              ,a.[Controller]
		                              ,a.[Action]
		                              ,b.Act_ID
		                            FROM [T_SysPage] a
		                              left join T_SysAction b on a.Page_ID=b.Page_ID
		                            where a.Controller=b.Controller and a.Action=b.Action 
			  
	                              ) as Page on Nav.Page_ID=Page.Page_ID
                              where active=1 and (Act_ID in(select Act_ID from T_Roles_Action where Role_ID=@Role_ID) or Parent_ID=-1)
                              order by OrderID Desc
                            ");
            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@Role_ID", SqlDbType.Int,4)
                    };
            parameters[0].Value = Role;
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            DingTalkAgent.Model.SysNav model;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysNav();
                if (dr["Nav_ID"].ToString() != "")
                {
                    model.Nav_ID = int.Parse(dr["Nav_ID"].ToString());
                }
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.Url = "/" + dr["Controller"].ToString() + "/" + dr["Action"].ToString();
                model.Ioc = dr["Ioc"].ToString();
                model.Alt = dr["Alt"].ToString();


                yield return model;
            }
        }
        /// <summary>
        /// 获得数据 T_SysNav分页列表
        /// </summary>
        /// <param name="PageIndex">页码</param>
        /// <param name="PageSize">页大小</param>
        /// <param name="total">总记录数</param>
        /// <param name="strWhere">查询语句</param>
        /// <returns></returns>
        public List<DingTalkAgent.Model.SysNav> GetPageList(int PageIndex, int PageSize, out int total, int Site_ID, int Parent_ID, string Name)
        {
            int Skip = (PageIndex - 1) * PageSize;
            //count
            StringBuilder strSqlCount = new StringBuilder();
            strSqlCount.Append("Select @total=count(Nav_ID) From T_SysNav a");
            //data
            StringBuilder strSql = new StringBuilder();
            strSql.Append("select Top " + PageSize + " * from (");
            strSql.Append("select  row_number() OVER (ORDER BY a.OrderID Desc) AS row, a.Nav_ID, a.active, a.Site_ID, a.Page_ID, a.Code, a.Parent_ID, a.Name, a.Ioc, a.Alt, a.OrderID,b.Name as PageName");
            strSql.Append(" FROM  T_SysNav a left join T_SysPage b on a.Page_ID=b.Page_ID ");
            //where
            StringBuilder Where = new StringBuilder();
            Where.Append(" where a.Site_ID=@Site_ID  ");
            if (Parent_ID != -1)
            {
                Where.Append(" and a.Parent_ID=@Parent_ID ");
            }
            if (Name.Trim() != "")
            {
                Where.Append(" and a.Name like @Name");
            }


            strSqlCount.Append(Where.ToString());
            strSql.Insert(0, strSqlCount.ToString() + ";");
            strSql.Append(Where.ToString());
            strSql.Append(") as a ");
            strSql.Append(" Where a.row>" + Skip);

            //parameters
            SqlParameter[] parameters = {
                    new SqlParameter("@total", SqlDbType.Int,4),
                    new SqlParameter("@Site_ID", SqlDbType.Int,4),
                    new SqlParameter("@Parent_ID", SqlDbType.Int,4),
                    new SqlParameter("@Name", SqlDbType.VarChar,32)
                    };
            parameters[0].Direction = ParameterDirection.Output;
            parameters[1].Value = Site_ID;
            parameters[2].Value = Parent_ID;
            parameters[3].Value = "%" + Name + "%";

            List<DingTalkAgent.Model.SysNav> list = new List<DingTalkAgent.Model.SysNav>();
            DingTalkAgent.Model.SysNav model = new DingTalkAgent.Model.SysNav();
            DataSet ds = DbHelperSQL.Query(strSql.ToString(), parameters);
            total = Convert.ToInt32(parameters[0].Value);

            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                model = new DingTalkAgent.Model.SysNav();
                if (dr["Nav_ID"].ToString() != "")
                {
                    model.Nav_ID = int.Parse(dr["Nav_ID"].ToString());
                }
                if (dr["active"].ToString() != "")
                {
                    if ((dr["active"].ToString() == "1") || (dr["active"].ToString().ToLower() == "true"))
                    {
                        model.active = true;
                    }
                    else
                    {
                        model.active = false;
                    }
                }
                if (dr["Site_ID"].ToString() != "")
                {
                    model.Site_ID = int.Parse(dr["Site_ID"].ToString());
                }
                if (dr["Page_ID"].ToString() != "")
                {
                    model.Page_ID = int.Parse(dr["Page_ID"].ToString());
                }
                model.Code = dr["Code"].ToString();
                if (dr["Parent_ID"].ToString() != "")
                {
                    model.Parent_ID = int.Parse(dr["Parent_ID"].ToString());
                }
                model.Name = dr["Name"].ToString();
                model.PageName = dr["PageName"].ToString();
                model.Ioc = dr["Ioc"].ToString();
                model.Alt = dr["Alt"].ToString();
                if (dr["OrderID"].ToString() != "")
                {
                    model.OrderID = int.Parse(dr["OrderID"].ToString());
                }

                list.Add(model);
            }
            return list;
        }
        #endregion
    }

}
